package com.credit.jumu.calculator.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.activity.CarResultActivity;
import com.credit.jumu.calculator.activity.CarResultActivity_;
import com.credit.jumu.calculator.activity.HouseResultActivity_;
import com.credit.jumu.calculator.base.BaseFragment;
import com.credit.jumu.calculator.bean.AddInfo;
import com.credit.jumu.calculator.bean.CarPriceInfo;
import com.credit.jumu.calculator.utils.AddressUtil;
import com.credit.jumu.calculator.utils.DensityUtil;
import com.credit.jumu.calculator.utils.JsonUtils;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.StringHandleUtil;
import com.credit.jumu.calculator.utils.ToastUtil;
import com.credit.jumu.calculator.utils.TypeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

import cn.qqtheme.framework.picker.OptionPicker;
import cn.qqtheme.framework.widget.WheelView;

/**
 * Created by Administrator on 2017/11/23/023.
 */
@EFragment(R.layout.fragment_car)
public class CarFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {
	String[] adds;
	int[] thired = new int[]{516, 746, 924, 1252, 1630};
	int[] huaall = new int[]{400, 570, 760, 1140};
	String addData;
	ArrayList<AddInfo> addInfos;
	int carair = -1;
	int city = -1;
	int third = 0;
	int scratch = 0;
	int cartherse = 516;
	int huan = 400;
	@ViewById
	CheckBox checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8;
	@ViewById
	TextView exhaust, chooseaddress, insuranceMoney, insuranceMoney5;
	@ViewById
	TextView insuranceMoney1, insuranceMoney2, insuranceMoney3, insuranceMoney4,
			insuranceMoney7, insuranceMoney8;
	@ViewById
	EditText carallmoney, fristmoney, needyear, carlilv, seatnumber;
	@ViewById
	LinearLayout allchoose;
	@ViewById
	RelativeLayout chooselayout;
	boolean isShowChoose = false;
	public ScaleAnimation animation;//显示
	public ScaleAnimation animation2;//隐藏

	@AfterViews
	public void initView() {
		animation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animation.setDuration(500);
//		animation2 = new ScaleAnimation(1.0f, 1.0f, 1.0f, 0.0f,
//				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
//		animation2.setDuration(500);
		allchoose.setVisibility(View.GONE);
		addInfos = new ArrayList<>();
		getAdd();
		checkBox1.setOnCheckedChangeListener(this);
		checkBox2.setOnCheckedChangeListener(this);
		checkBox3.setOnCheckedChangeListener(this);
		checkBox4.setOnCheckedChangeListener(this);
		checkBox5.setOnCheckedChangeListener(this);
		checkBox6.setOnCheckedChangeListener(this);
		checkBox7.setOnCheckedChangeListener(this);
		checkBox8.setOnCheckedChangeListener(this);
		chooselayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isShowChoose) {
					allchoose.setVisibility(View.GONE);
				} else {
					allchoose.setVisibility(View.VISIBLE);
					allchoose.setAnimation(animation);
					animation.startNow();
				}
				isShowChoose = !isShowChoose;
			}
		});
		seatnumber.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				int num = 0;
				try {
					num = Integer.parseInt(seatnumber.getText().toString());
				} catch (NumberFormatException e) {
					e.printStackTrace();
					num = 0;
				}
				insuranceMoney8.setText(num * 50 + "");

			}
		});
		carallmoney.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				LogUtils.println("输入完成3" + carallmoney.getText().toString());
				insuranceMoney3.setText(StringHandleUtil.toOnlyHavaTwo((StringHandleUtil.toInt(carallmoney.getText().toString())) * 10000 * 0.012) + "");
				insuranceMoney4.setText(StringHandleUtil.toOnlyHavaTwo((StringHandleUtil.toInt(carallmoney.getText().toString())) * 10000 * 0.0025) + "");
				insuranceMoney7.setText(StringHandleUtil.toOnlyHavaTwo((StringHandleUtil.toInt(carallmoney.getText().toString())) * 10000 * 0.01) + "");
				//third
				int cartherse = thired[third];
				double all = (cartherse + (StringHandleUtil.toInt(carallmoney.getText().toString()) * 10000 * 0.012)) * 0.2;
				insuranceMoney1.setText(StringHandleUtil.toOnlyHavaTwo(all));
				insuranceMoney2.setText(StringHandleUtil.toOnlyHavaTwo(cartherse * 0.2));
			}
		});
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();
		carair = -1;
		city = -1;
		exhaust.setText("请选择");
		chooseaddress.setText("请选择");
		exhaust.setTextColor(ContextCompat.getColor(getContext(), R.color.showtextbg));
		chooseaddress.setTextColor(ContextCompat.getColor(getContext(), R.color.showtextbg));
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		carair = -1;
		city = -1;
		exhaust.setTextColor(ContextCompat.getColor(getContext(), R.color.showtextbg));
		chooseaddress.setTextColor(ContextCompat.getColor(getContext(), R.color.showtextbg));
		exhaust.setText("请选择");
		chooseaddress.setText("请选择");
	}

	@Background
	public void getAdd() {
		addInfos.clear();
		addData = AddressUtil.readFromAssets(getContext());
		if (!TextUtils.isEmpty(addData)) {
			JSONArray allData = JsonUtils.getJsonArray(addData);
			LogUtils.println("数据长度" + allData.size());
			adds = new String[allData.size()];

			for (int i = 0; i < allData.size(); i++) {

				AddInfo addInfo = new AddInfo();
				JSONObject addData = allData.getJSONObject(i);
				addInfo.setId(TypeUtils.getJsonString(addData, "id"));
				addInfo.setLocation(TypeUtils.getJsonString(addData, "location"));
				adds[i] = TypeUtils.getJsonString(addData, "location");
				JSONArray vusData = TypeUtils.getJsonArray(addData, "vus");
				ArrayList<CarPriceInfo> carPriceInfos = new ArrayList<>();
				for (int j = 0; j < vusData.size(); j++) {
					CarPriceInfo carPricce = new CarPriceInfo();
					JSONObject vusDataInfo = vusData.getJSONObject(j);
					carPricce.setCar_displacement(TypeUtils.getJsonString(vusDataInfo, "car_displacement"));
					carPricce.setPrice(TypeUtils.getJsonString(vusDataInfo, "price"));
					carPriceInfos.add(carPricce);

				}
				addInfo.setCarPriceInfo(carPriceInfos);
				addInfos.add(addInfo);
			}
		}

	}

	//城市
	@Click(R.id.chooseaddress)
	public void chooseAdds() {
		OptionPicker picker = new OptionPicker(getActivity(), adds);
		picker.setCycleDisable(true);//设置启用循环
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setSubmitText("完成");
		picker.setCanceledOnTouchOutside(true);
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {
				city = index;
				chooseaddress.setText(item);
				chooseaddress.setTextColor(ContextCompat.getColor(getContext(), R.color.choosetextbg));

			}
		});
		picker.show();
	}

	@Click(R.id.togetresult)
	public void getResult() {
		//车船使用税

		double carallmoneyd = StringHandleUtil.toInt(carallmoney.getText().toString());//总价
		double fristmoneyd = StringHandleUtil.toInt(fristmoney.getText().toString());//首付比例
		double needyeard = StringHandleUtil.toInt(needyear.getText().toString());//还款年限
		double carlilvd = StringHandleUtil.toInt(carlilv.getText().toString());//贷款利率
		double seatnumberd = StringHandleUtil.toInt(seatnumber.getText().toString());//车座量
		//所有的险
		double mian = StringHandleUtil.toInt(insuranceMoney1.getText().toString());//免赔
		double wuguo = StringHandleUtil.toInt(insuranceMoney2.getText().toString());//无过
		double sunshi = StringHandleUtil.toInt(insuranceMoney3.getText().toString());//损失
		double ziran = StringHandleUtil.toInt(insuranceMoney4.getText().toString());//自然
		double qiangdao = StringHandleUtil.toInt(insuranceMoney7.getText().toString());//抢到
		double renyuan = StringHandleUtil.toInt(insuranceMoney8.getText().toString());//人员

		if (!checkBox1.isChecked()) {
			cartherse = 0;
		}
		if (!checkBox2.isChecked()) {
			mian = 0;
		}
		if (!checkBox3.isChecked()) {
			wuguo = 0;
		}
		if (!checkBox4.isChecked()) {
			sunshi = 0;
		}
		if (!checkBox5.isChecked()) {
			ziran = 0;
		}
		if (!checkBox6.isChecked()) {
			huan = 0;
		}
		if (!checkBox7.isChecked()) {
			qiangdao = 0;
		}
		if (!checkBox8.isChecked()) {
			renyuan = 0;
		}
		double allXian = cartherse + huan + mian + wuguo + sunshi + ziran + qiangdao + renyuan;

		if (carair < 0 || city < 0 || carallmoneyd <= 0 || fristmoneyd <= 0 || needyeard <= 0 || carlilvd <= 0 || seatnumberd <= 0) {
			ToastUtil.showShortToast("请先完善信息");
			return;
		}
		LogUtils.println("城市" + city + "排量" + carair);
		String ccsys = addInfos.get(city).getCarPriceInfo().get(carair).getPrice();
		LogUtils.println("车船使用税" + ccsys);
		Intent intent = new Intent(getActivity(), CarResultActivity_.class);
		intent.putExtra("carallmoneyd", carallmoneyd);
		intent.putExtra("fristmoneyd", fristmoneyd);//
		intent.putExtra("needyeard", needyeard);//
		intent.putExtra("carlilvd", carlilvd);//
		intent.putExtra("seatnumberd", seatnumberd);//
		intent.putExtra("ccsys", ccsys);//
		intent.putExtra("allXian", allXian);//

		startActivity(intent);
	}

	//三方责任险
	@Click(R.id.insuranceMoney)
	public void insuranceMoney() {

		OptionPicker picker = new OptionPicker(getActivity(), new String[]{
				"5万保额516元", "10万保额746元", "20万保额924元", "50万保额1252元"
				, "100万保额1630元"
		});
		picker.setCycleDisable(true);//设置启用循环
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setSubmitText("完成");


		picker.setCanceledOnTouchOutside(true);
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {
				third = index;
				insuranceMoney.setText(item + "");
				cartherse = thired[third];
				double all = (cartherse + (StringHandleUtil.toInt(carallmoney.getText().toString()) * 10000 * 0.012)) * 0.2;
				insuranceMoney1.setText(StringHandleUtil.toOnlyHavaTwo(all));
				insuranceMoney2.setText(StringHandleUtil.toOnlyHavaTwo(cartherse * 0.2));
			}
		});
		picker.show();
	}

	//车身险
	@Click(R.id.insuranceMoney5)
	public void insuranceMoney5() {

		OptionPicker picker = new OptionPicker(getActivity(), new String[]{
				"2千保额400元", "5千保额570元", "1万保额760元", "2万保额1140元"

		});
		picker.setCycleDisable(true);//设置启用循环
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setSubmitText("完成");

		picker.setCanceledOnTouchOutside(true);
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {
				insuranceMoney5.setText(item + "");
				scratch = index;
				huan = huaall[index];
			}
		});
		picker.show();
	}

	//排量
	@Click(R.id.exhaust)
	public void exhausteClick() {

		OptionPicker picker = new OptionPicker(getActivity(), new String[]{
				"1.0升以上至1.6升(含)", "1.6升以上至2.0升(含)", "2.0升以上至2.5升(含)", "2.5升以上至3.0升(含)"
				, "3.0升以上至4.0升(含)", "4.0升以上"
		});
		picker.setCycleDisable(true);//设置启用循环
		picker.setLineSpaceMultiplier(1);//总高度(1-4)
		picker.setCanceledOnTouchOutside(false);
		picker.setDividerRatio(WheelView.DividerConfig.FILL);
		picker.setShadowColor(Color.BLACK, 1);//选中的背景色
		picker.setSelectedIndex(0);
		picker.setTopLineColor(getResources().getColor(R.color.transparent));//分割线颜色
		picker.setSubmitTextColor(getResources().getColor(R.color.titlettextbg));//右边文字
		picker.setCancelTextColor(getResources().getColor(R.color.titlettextbg));//左边文字
		picker.setTextSize(DensityUtil.dip2px(getContext(), 7));//文字大小
		picker.setTextColor(getResources().getColor(R.color.back));
		picker.setDividerColor(getResources().getColor(R.color.transparent));
		picker.setTopBackgroundColor(getResources().getColor(R.color.linebg));//顶部背景
		picker.setSubmitText("完成");


		picker.setCanceledOnTouchOutside(true);
		picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
			@Override
			public void onOptionPicked(int index, String item) {
				carair = index;
				exhaust.setText(item + "");
				exhaust.setTextColor(ContextCompat.getColor(getContext(), R.color.choosetextbg));
			}
		});
		picker.show();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
			case R.id.checkBox1:
				if (isChecked) {
					checkBox2.setChecked(true);
					checkBox3.setChecked(true);
				} else {
					checkBox2.setChecked(false);
					checkBox3.setChecked(false);
				}
				break;
			case R.id.checkBox2:
				if (!checkBox1.isChecked()) {
					ToastUtil.showShortToast("请先选择主险第三方责任险");
					checkBox2.setChecked(false);
				}
				break;
			case R.id.checkBox3:
				if (!checkBox1.isChecked()) {
					ToastUtil.showShortToast("请先选择主险第三方责任险");
					checkBox3.setChecked(false);
				}
				break;
			case R.id.checkBox4:
				if (isChecked) {
					checkBox5.setChecked(true);
					checkBox6.setChecked(true);
				} else {
					checkBox5.setChecked(false);
					checkBox6.setChecked(false);
				}
				break;
			case R.id.checkBox5:
				if (!checkBox4.isChecked()) {
					ToastUtil.showShortToast("请先选择主险车辆损失险");
					checkBox5.setChecked(false);
				}
				break;
			case R.id.checkBox6:
				if (!checkBox4.isChecked()) {
					ToastUtil.showShortToast("请先选择主险车辆损失险");
					checkBox6.setChecked(false);
				}
				break;
		}
	}
}
